package com.hrms.controller.user;

import com.hrms.annotation.UserIgnore;
import com.hrms.entity.User;
import com.hrms.http.ResponseResult;
import com.hrms.service.UserService;
import com.hrms.utils.EntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Transactional
@RestController
@RequestMapping("/user/center")
public class UserCenterController {
    // 用户个人中心

    @Autowired
    private UserService userService;

    @UserIgnore
    @RequestMapping("/login")
    public ResponseResult<String> login(@RequestBody EntityUtil entity) {
        // 用户登录
        User user = new User();
        user.setUser_name(entity.getUser_name());
        user.setUser_password(entity.getUser_password());

        // 账号是否存在
        if(!userService.nameExist(user)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR_ACCOUNT", null);
        }

        // 账号密码是否匹配
        if(!userService.passwordisRight(user)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR_PASSWORD", null);
        }

        // 检查账号是否离线
        User userBefore = new User();
        userBefore.setUser_name(entity.getUser_name());
        userBefore.setUser_status("离线");

        if(!userService.checkStatuebyName(userBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改账号状态
        User userAfter = new User();
        userAfter.setUser_name(entity.getUser_name());
        userAfter.setUser_status("在线");

        if(!userService.updateStatuebyName(userAfter)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        String user_id = userService.selectUserId(user);
        return new ResponseResult<>("SUCCESS", user_id);
    }

    @UserIgnore
    @RequestMapping("register")
    public ResponseResult<Object> register(@RequestBody EntityUtil entity) {
        // 用户注册

        User user = new User();
        user.setUser_name(entity.getUser_name());
        user.setUser_password(entity.getUser_password());
        user.setUser_status("离线");
        user.setCustomer_id("无");

        // 账号名是否存在
        if(userService.nameExist(user)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR_ACCOUNT", null);
        }

        // 新建用户
        if(!userService.addUser(user)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("/exit")
    public ResponseResult<Object> exit(@RequestBody EntityUtil entity) {
        // 用户退出

        // 检查账号是否在线
        User userBefore = new User();
        userBefore.setUser_id(entity.getUser_id());
        userBefore.setUser_status("在线");

        if(!userService.checkStatue(userBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改账号状态
        User userAfter = new User();
        userAfter.setUser_id(entity.getUser_id());
        userAfter.setUser_status("离线");

        if(!userService.updateStatue(userAfter)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }
}
